<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <!-- 左侧内容 -->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 放置下拉菜单 -->
          <el-dropdown @command="operateDepts">
            <!-- 内容 -->
            <span
              >操作
              <i class="el-icon-arrow-down" />
            </span>
            <!-- 具名插槽 -->
            <el-dropdown-menu slot="dropdown">
              <!-- 下拉选项 -->
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="edit" v-if="!isRoot"
                >编辑部门</el-dropdown-item
              >
              <el-dropdown-item command="del" v-if="!isRoot"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>

      <!-- 右侧内容 -->
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentAPI } from "@/api/departments";
export default {
  props: {
    // 定义一个传入的属性
    treeNode: {
      required: true,
      type: Object,
    },
    isRoot: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    // 点击新增、编辑、删除
    async operateDepts(type) {
      // 添加
      if (type === "add") {
        // 触发自定义事件，告诉父组件，显示弹层
        this.$emit("addDepts", this.treeNode);
      }
      // 编辑
      else if (type === "edit") {
        // 触发自定义事件，告诉父组件，编辑
        this.$emit("editDepts", this.treeNode);
      }
      // 删除
      else {
        // 弹出警示框 - 点击确定 - 删除数据 - 通知父组件重新获取数据
        this.$confirm("确定要删除该部门吗？")
          .then(() => {
            return delDepartmentAPI(this.treeNode.id);
          })
          .then(() => {
            // 通知父组件重新获取数据
            this.$emit("delDepts");
            // 提示删除成功
            this.$message.success("删除部门成功");
          });
      }
    },
  },
};
</script>

<style>
</style>